//
//  HealthSetView.m
//  project
//
//  Created by 圆圆陈 on 2017/12/22.
//  Copyright © 2017年 黄建华. All rights reserved.
//

#import "HealthSetView.h"

@interface HealthSetView()
@property(nonatomic,strong)NSString *returnStr;
@property(nonatomic,strong)NSMutableArray *timeBtnArr;
@property(nonatomic,strong)NSMutableArray *weekBtnArr;
@end

@implementation HealthSetView
{
    UIView *backView;
    UIDatePicker *startPicker;
    UIDatePicker *endPicker;
}
-(instancetype)initWithFrame:(CGRect)frame withType:(NSInteger)type{
    if (self=[super initWithFrame:frame]) {
        _type = type;
        _returnStr = [NSString string];
        _timeBtnArr = [NSMutableArray array];
        _weekBtnArr = [NSMutableArray array];
        backView = [[UIView alloc]initWithFrame:CGRectMake(0, HHHHEIGHT/2, HHHWIDTH, HHHHEIGHT/2)];
        backView.backgroundColor = [UIColor whiteColor];
        [self addSubview:backView];
        
        UIButton *cancelBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        cancelBtn.frame = CGRectMake(FitW(10), FitH(10), FitW(50), FitH(30));
        [cancelBtn setTitleNormal:HHH_Text(@"退出")];
        [cancelBtn setTitleColorNormal:SNCOLORBLACK];
        [cancelBtn addTouchUpTarget:self action:@selector(cancelBtnClick)];
        cancelBtn.titleLabel.font = [UIFont systemFontOfSize:FitFont(17)];
        [backView addSubview:cancelBtn];
        
        UIButton *sureBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        sureBtn.frame = CGRectMake(HHHWIDTH-FitW(70), FitH(10), FitW(50), FitH(30));
        [sureBtn setTitleNormal:HHH_Text(@"确定")];
        [sureBtn setTitleColorNormal:SNCOLORBLACK];
        [sureBtn addTouchUpTarget:self action:@selector(sureBtnClick)];
        sureBtn.titleLabel.font = [UIFont systemFontOfSize:FitFont(17)];
        [backView addSubview:sureBtn];
        
        UIView *line = [[UIView alloc]initWithFrame:CGRectMake(0, FitH(50),HHHWIDTH , 1)];
        line.backgroundColor = SNCOLORGRAYLINE;
        [backView addSubview:line];
        
        UILabel *timeLabel = [[UILabel alloc]initWithFrame:CGRectMake(5, line.bottom+FitH(12), FitW(100) +5, FitH(26))];
        timeLabel.textColor = SNCOLORBLACK;
        timeLabel.text = HHH_Text(@"时间间隔");
        timeLabel.font = [UIFont systemFontOfSize:FitFont(17)];
        [backView addSubview:timeLabel];
        
        NSArray *arr = @[@"半小时",@"1小时",@"2小时",@"3小时"];
        CGFloat width = (HHHWIDTH-timeLabel.right-FitW(20))/arr.count;
        for (int i=0; i<arr.count; i++) {
            UIButton *timeBtn = [UIButton buttonWithType:UIButtonTypeCustom];
            timeBtn.frame = CGRectMake(timeLabel.right+FitW(10)+width*i,line.bottom+FitH(10), width, FitH(30));
            timeBtn.tag = 100+i;
            timeBtn.layer.borderWidth=1;
            timeBtn.layer.borderColor = SNCOLORGRAY.CGColor;
            [timeBtn setTitleColorNormal:SNCOLORBLACK];
            [timeBtn setTitleColorSelecte:SNCOLORWHITE];
            [timeBtn setBackgroundImage:[UIImage imageWithColor:SNCOLORBLACK] forState:UIControlStateSelected];
            [timeBtn setTitleNormal:arr[i]];
            timeBtn.titleLabel.font = [UIFont systemFontOfSize:FitFont(17)];
            [timeBtn addTouchUpTarget:self action:@selector(timeBtnClick:)];
            [backView addSubview:timeBtn];
            [_timeBtnArr addObject:timeBtn];
            if (_type==0) {
                if (timeBtn.tag==100+(SN_USER.drinkinterval/30-1)) {
                    timeBtn.selected = YES;
                }
            }else{
                if (timeBtn.tag==100+(SN_USER.sedentaryinterval/30-1)) {
                    timeBtn.selected = YES;
                }
            }
        }
        NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
        [formatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"]; // 这里是用大写的 H
        NSString* dateStr = [formatter stringFromDate:[NSDate date]];
        NSDate *date = [formatter dateFromString:dateStr];
        
        startPicker = [[UIDatePicker alloc] initWithFrame:CGRectMake(0, line.bottom+FitH(44), HHHWIDTH/2,backView.height-FitH(220))];
        startPicker.datePickerMode = UIDatePickerModeTime;
        startPicker.backgroundColor = [UIColor clearColor];
        startPicker.tag = 100;
        startPicker.date = date;
        startPicker.locale = [NSLocale localeWithLocaleIdentifier:@"en_GB"];
        [startPicker addTarget:self
                             action:@selector(pickerChanged:)
                   forControlEvents:UIControlEventValueChanged];
        [startPicker setValue:SNCOLORBLACK forKey:@"textColor"]; //设置字体颜色
        [backView addSubview:startPicker];
        
        NSArray *arr1 = @[@":",@"--",@":"];
        for (int i=0; i<arr1.count; i++) {
            UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, FitW(20), FitW(20))];
            label.center = CGPointMake(HHHWIDTH/4*(i+1), startPicker.centerY);
            label.text = arr1[i];
            label.textColor =SNCOLORBLACK;
            [backView addSubview:label];
        }
        
        endPicker = [[UIDatePicker alloc] initWithFrame:CGRectMake(HHHWIDTH/2, line.bottom+FitH(44), HHHWIDTH/2,backView.height-FitH(220))];
        endPicker.datePickerMode = UIDatePickerModeTime;
        endPicker.backgroundColor = [UIColor clearColor];
        endPicker.tag = 101;
        endPicker.date = date;
        endPicker.locale = [NSLocale localeWithLocaleIdentifier:@"en_GB"];
        [endPicker addTarget:self
                        action:@selector(pickerChanged:)
              forControlEvents:UIControlEventValueChanged];
        [endPicker setValue:SNCOLORBLACK forKey:@"textColor"]; //设置字体颜色
        [backView addSubview:endPicker];
    
        NSString *tmpStr = [dateStr substringWithRange:NSMakeRange(0, 11)];
        if (_type==0) {
            NSString *startStr = [NSString stringWithFormat:@"%@%02ld:%02ld:00",tmpStr,SN_USER.drinkStartHour1,SN_USER.drinkStartMins1];
            startPicker.date =[formatter dateFromString:startStr];
            
            NSString *endStr = [NSString stringWithFormat:@"%@%02ld:%02ld:00",tmpStr,SN_USER.drinkEndHour1,SN_USER.drinkEndMins1];
            endPicker.date =[formatter dateFromString:endStr];
            endPicker.minimumDate = startPicker.date;
        }else{
            NSString *startStr = [NSString stringWithFormat:@"%@%02ld:%02ld:00",tmpStr,SN_USER.sedentaryStartHour1,SN_USER.sedentaryStartMins1];
            startPicker.date =[formatter dateFromString:startStr];
            
            NSString *endStr = [NSString stringWithFormat:@"%@%02ld:%02ld:00",tmpStr,SN_USER.sedentaryEndHour1,SN_USER.sedentaryEndMins1];
            endPicker.date =[formatter dateFromString:endStr];
            endPicker.minimumDate = startPicker.date;
        }
        
        UILabel *repeatLabel = [[UILabel alloc]initWithFrame:CGRectMake(FitW(10), endPicker.bottom+FitH(12), HHHWIDTH, FitH(20))];
        repeatLabel.textColor = SNCOLORBLACK;
        repeatLabel.text = HHH_Text(@"选择重复周期");
        repeatLabel.font = [UIFont systemFontOfSize:FitFont(17)];
        [backView addSubview:repeatLabel];
        
        NSArray *weekArr = @[HHH_Text(@"周日"),HHH_Text(@"周一"),HHH_Text(@"周二"),HHH_Text(@"周三"),HHH_Text(@"周四"),HHH_Text(@"周五"),HHH_Text(@"周六")];
        CGFloat width1 =(HHHWIDTH-FitW(20))/weekArr.count;
        for (int i=0; i<weekArr.count; i++) {
            UIButton *weekBtn = [UIButton buttonWithType:UIButtonTypeCustom];
            weekBtn.frame = CGRectMake(FitW(10)+width1*i,repeatLabel.bottom+FitH(20), width1, FitH(30));
            weekBtn.tag = 100+i;
            weekBtn.layer.borderWidth=1;
            weekBtn.layer.borderColor = SNCOLORGRAY.CGColor;
            [weekBtn setTitleColorNormal:SNCOLORBLACK];
            [weekBtn setTitleColorSelecte:SNCOLORWHITE];
            [weekBtn setBackgroundImage:[UIImage imageWithColor:SNCOLORBLACK] forState:UIControlStateSelected];
            [weekBtn setTitleNormal:weekArr[i]];
            weekBtn.titleLabel.font = [UIFont systemFontOfSize:FitFont(17)];
            [weekBtn addTouchUpTarget:self action:@selector(weekBtnClick:)];
            [backView addSubview:weekBtn];
            if (_type==0) {
                switch (weekBtn.tag) {
                    case 100:
                        weekBtn.selected = SN_USER.drinkSUN ;
                        break;
                    case 101:
                        weekBtn.selected = SN_USER.drinkMON ;
                        break;
                    case 102:
                        weekBtn.selected = SN_USER.drinkTUE ;
                        break;
                    case 103:
                        weekBtn.selected = SN_USER.drinkWED ;
                        break;
                    case 104:
                        weekBtn.selected = SN_USER.drinkTHU ;
                        break;
                    case 105:
                        weekBtn.selected = SN_USER.drinkFRI ;
                        break;
                    case 106:
                        weekBtn.selected = SN_USER.drinkSAT ;
                        break;
                    default:
                        break;
                }
            }else{
                switch (weekBtn.tag) {
                    case 100:
                        weekBtn.selected = SN_USER.sedentarySUN ;
                        break;
                    case 101:
                        weekBtn.selected = SN_USER.sedentaryMON ;
                        break;
                    case 102:
                        weekBtn.selected = SN_USER.sedentaryTUE ;
                        break;
                    case 103:
                        weekBtn.selected = SN_USER.sedentaryWED ;
                        break;
                    case 104:
                        weekBtn.selected = SN_USER.sedentaryTHU ;
                        break;
                    case 105:
                        weekBtn.selected = SN_USER.sedentaryFRI ;
                        break;
                    case 106:
                        weekBtn.selected = SN_USER.sedentarySAT ;
                        break;
                    default:
                        break;
                }
            }
        }
    }
    [self changeTimeBtnStatus];
    return self;
}
- (void)tapTouch
{
    SN_USER = [SNUserInfoModel getUserInfoFromDBWithUserID:[KK_LastUser getObjectValue]];
    [self animationedDown];
}
-(void)cancelBtnClick{
    SN_USER = [SNUserInfoModel getUserInfoFromDBWithUserID:[KK_LastUser getObjectValue]];
    [self animationedDown];
}
-(void)sureBtnClick{

    for (UIButton *btn in _timeBtnArr) {
        if (btn.selected) {
           _returnStr = [NSString stringWithFormat:@"每%@提醒一次",btn.titleLabel.text];
        }
    }
    NSDateFormatter *formatter = [[NSDateFormatter alloc]init];
    [formatter setDateFormat:@"HH:mm"];
    NSString *start = [formatter stringFromDate:startPicker.date];
    NSString *end = [formatter stringFromDate:endPicker.date];
    
    _returnStr = [_returnStr stringByAppendingString:[NSString stringWithFormat:@"(%@-%@)",start,end]];
    if (self.sureButtonBlock) {
        self.sureButtonBlock(_returnStr);
    }
    if (_type==0) {
        SN_USER.drinkStartHour1 = [start substringWithRange:NSMakeRange(0, 2)].integerValue;
        SN_USER.drinkStartMins1  =[start substringWithRange:NSMakeRange(3, 2)].integerValue;
        SN_USER.drinkEndHour1 = [end substringWithRange:NSMakeRange(0, 2)].integerValue;
        SN_USER.drinkEndMins1  =[end substringWithRange:NSMakeRange(3, 2)].integerValue;
        for (UIButton *weekBtn in _weekBtnArr) {
            if (weekBtn.selected) {
                SN_USER.drinkOnce = NO;
            }
        }
        [SN_EveryDayModelHelp startDrinkWater];
    }else{
        SN_USER.sedentaryStartHour1 = [start substringWithRange:NSMakeRange(0, 2)].integerValue;
        SN_USER.sedentaryStartMins1  =[start substringWithRange:NSMakeRange(3, 2)].integerValue;
        SN_USER.sedentaryEndHour1 = [end substringWithRange:NSMakeRange(0, 2)].integerValue;
        SN_USER.sedentaryEndMins1 =[end substringWithRange:NSMakeRange(3, 2)].integerValue;
        for (UIButton *weekBtn in _weekBtnArr) {
            if (weekBtn.selected) {
                 SN_USER.sedentaryOnce = NO;
            }
        }
        [SN_EveryDayModelHelp startsedentary];
    }
    [SN_USER updateToDB];
    [self animationedDown];
}
-(void)timeBtnClick:(UIButton *)sender{
    sender.selected = !sender.selected;
    if (sender.selected) {
        for (UIButton *btn in _timeBtnArr) {
            if (btn.tag!=sender.tag) {
                btn.selected = !sender.selected;
            }
        }
        if (_type==0) {
            SN_USER.drinkinterval = 30*(sender.tag-100+1);
        }else{
            SN_USER.sedentaryinterval =30*(sender.tag-100+1);
        }
    }
}
-(void)pickerChanged:(UIDatePicker *)picker{
    endPicker.minimumDate = startPicker.date;
    [self changeTimeBtnStatus];
}
-(void)changeTimeBtnStatus{
    NSTimeInterval date1 = [startPicker.date timeIntervalSinceReferenceDate];
    NSTimeInterval date2 = [endPicker.date timeIntervalSinceReferenceDate];
    int min = (date2 - date1)/60;
    for (int i=0; i<_timeBtnArr.count; i++) {
        UIButton *timeBtn =_timeBtnArr[i];
        timeBtn.layer.borderWidth=1;
        if (min<30) {
            timeBtn.userInteractionEnabled = NO;
            timeBtn.backgroundColor = SNCOLORGRAY;
            timeBtn.selected = NO;
            [timeBtn setTitleColorNormal:SNCOLORWHITE];
            timeBtn.layer.borderColor = SNCOLORWHITE.CGColor;
        }else if (min<60){
            if (i<1) {
                timeBtn.userInteractionEnabled = YES;
                timeBtn.backgroundColor = SNCOLORWHITE;
                [timeBtn setTitleColorNormal:SNCOLORBLACK];
                timeBtn.layer.borderColor = SNCOLORGRAY.CGColor;
            }else{
                timeBtn.userInteractionEnabled = NO;
                timeBtn.backgroundColor = SNCOLORGRAY;
                timeBtn.selected = NO;
                [timeBtn setTitleColorNormal:SNCOLORWHITE];
                timeBtn.layer.borderColor = SNCOLORWHITE.CGColor;
            }
        }else if (min<120){
            if (i<2) {
                timeBtn.userInteractionEnabled = YES;
                timeBtn.backgroundColor = SNCOLORWHITE;
                [timeBtn setTitleColorNormal:SNCOLORBLACK];
                timeBtn.layer.borderColor = SNCOLORGRAY.CGColor;
            }else{
                timeBtn.userInteractionEnabled = NO;
                timeBtn.backgroundColor = SNCOLORGRAY;
                timeBtn.selected = NO;
                [timeBtn setTitleColorNormal:SNCOLORWHITE];
                timeBtn.layer.borderColor = SNCOLORWHITE.CGColor;
            }
        }else if (min<180){
            if (i<3) {
                timeBtn.userInteractionEnabled = YES;
                timeBtn.backgroundColor = SNCOLORWHITE;
                [timeBtn setTitleColorNormal:SNCOLORBLACK];
                timeBtn.layer.borderColor = SNCOLORGRAY.CGColor;
            }else{
                timeBtn.userInteractionEnabled = NO;
                timeBtn.backgroundColor = SNCOLORGRAY;
                timeBtn.selected = NO;
                [timeBtn setTitleColorNormal:SNCOLORWHITE];
                timeBtn.layer.borderColor = SNCOLORWHITE.CGColor;
            }
        }else{
            timeBtn.userInteractionEnabled = YES;
            timeBtn.backgroundColor = SNCOLORWHITE;
            [timeBtn setTitleColorNormal:SNCOLORBLACK];
            timeBtn.layer.borderColor = SNCOLORGRAY.CGColor;
        }
    }
}
-(void)weekBtnClick:(UIButton *)sender{
    sender.selected = !sender.selected;
    if (sender.selected) {
        [_weekBtnArr addObject:sender];
    }else{
        [_weekBtnArr removeObject:sender];
    }
    if (_type==0) {
        if (sender.selected) {
            switch (sender.tag) {
                case 100:
                    SN_USER.drinkSUN = YES;
                    break;
                case 101:
                    SN_USER.drinkMON = YES;
                    break;
                case 102:
                    SN_USER.drinkTUE = YES;
                    break;
                case 103:
                    SN_USER.drinkWED = YES;
                    break;
                case 104:
                    SN_USER.drinkTHU = YES;
                    break;
                case 105:
                    SN_USER.drinkFRI = YES;
                    break;
                case 106:
                    SN_USER.drinkSAT = YES;
                    break;
                default:
                    break;
            }
        }else{
            switch (sender.tag) {
                case 100:
                    SN_USER.drinkSUN = NO;
                    break;
                case 101:
                    SN_USER.drinkMON = NO;
                    break;
                case 102:
                    SN_USER.drinkTUE = NO;
                    break;
                case 103:
                    SN_USER.drinkWED = NO;
                    break;
                case 104:
                    SN_USER.drinkTHU = NO;
                    break;
                case 105:
                    SN_USER.drinkFRI = NO;
                    break;
                case 106:
                    SN_USER.drinkSAT = NO;
                    break;
                default:
                    break;
            }
          }
        }
        else{
            if (sender.selected){
                switch (sender.tag) {
                    case 100:
                        SN_USER.sedentarySUN = YES;
                        break;
                    case 101:
                        SN_USER.sedentaryMON = YES;
                        break;
                    case 102:
                        SN_USER.sedentaryTUE = YES;
                        break;
                    case 103:
                        SN_USER.sedentaryWED = YES;
                        break;
                    case 104:
                        SN_USER.sedentaryTHU = YES;
                        break;
                    case 105:
                        SN_USER.sedentaryFRI = YES;
                        break;
                    case 106:
                        SN_USER.sedentarySAT = YES;
                        break;
                    default:
                        break;
                }
            }else{
                switch (sender.tag) {
                    case 100:
                        SN_USER.sedentarySUN = NO;
                        break;
                    case 101:
                        SN_USER.sedentaryMON = NO;
                        break;
                    case 102:
                        SN_USER.sedentaryTUE = NO;
                        break;
                    case 103:
                        SN_USER.sedentaryWED = NO;
                        break;
                    case 104:
                        SN_USER.sedentaryTHU = NO;
                        break;
                    case 105:
                        SN_USER.sedentaryFRI = NO;
                        break;
                    case 106:
                        SN_USER.sedentarySAT = NO;
                        break;
                    default:
                        break;
                }
        }
    }
}
-(BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch{
    if ([touch.view isDescendantOfView:backView]) {
        return NO;
    }
    return YES;
}
@end
